<?php
#注册插件
RegisterPlugin("Xd_touxiang","ActivePlugin_Xd_touxiang");

function ActivePlugin_Xd_touxiang() {

	Add_Filter_Plugin('Filter_Plugin_Mebmer_Avatar', 'Xd_touxiang_Url');

}

function Xd_touxiang_SubMenu($id){
	$arySubMenu = array(
		0 => array('基本配置', 'jbsz', 'left', false),
		1 => array('图片上传', 'tup', 'left', false),
	);
	foreach($arySubMenu as $k => $v){
		echo '<li><a href="?act='.$v[1].'" '.($v[3]==true?'target="_blank"':'').' class="'.($id==$v[1]?'on':'').'">'.$v[0].'</a></li>';
	}
}

//用的这个
function Xd_touxiang_get( $email,$s = '80', $d = 'local_rg', $r = 'G', $img = null, $atts = array()) {
	global $zbp;
	$url = '';
	if($zbp->Config('Xd_touxiang')->txxz == "a"){
		if($zbp->Config('Xd_touxiang')->jtxz == "a"){
		$local_rg = $zbp->host . 'zb_users/plugin/Xd_touxiang/touxiang/touxiang'.mt_rand(1,24).'.jpg';
		}
		if($zbp->Config('Xd_touxiang')->jtxz == "b"){
		$local_rg = $zbp->host . 'zb_users/plugin/Xd_touxiang/touxiang/touxiang'.mt_rand(25,45).'.jpg';
		}
		if($zbp->Config('Xd_touxiang')->jtxz == "c"){
		$local_rg = $zbp->host . 'zb_users/plugin/Xd_touxiang/touxiang/touxiang'.mt_rand(1,45).'.jpg';
		}
	}
	if($zbp->Config('Xd_touxiang')->txxz == "b"){
		$local_rg = $zbp->host . 'zb_users/plugin/Xd_touxiang/touxiang/touxiang'.mt_rand(46,58).'.gif';
	}
	if($zbp->Config('Xd_touxiang')->txxz == "c"){
		if($zbp->Config('Xd_touxiang')->diyjtxz == "a"){
		$local_rg = $zbp->host . 'zb_users/plugin/Xd_touxiang/touxiang/diy/diy'.mt_rand(1,10).'.jpg';
		}
		if($zbp->Config('Xd_touxiang')->diyjtxz == "b"){
		$local_rg = $zbp->host . 'zb_users/plugin/Xd_touxiang/touxiang/diy/diy'.mt_rand(11,20).'.gif';
		}
	}
	$local_g = $zbp->Config('Xd_touxiang')->dd?$zbp->Config('Xd_touxiang')->dd:$zbp->host.'zb_users/avatar/0.png';
	if (!$email || $email == 'null@null.com'){
		if($zbp->Config('Xd_touxiang')->d == "local_rg"){
			$url = $local_rg;
		}else{
			$url = $local_g;
		}
	}else{
		$urlencode_local_rg = urlencode($local_rg);
		$urlencode_local_g = urlencode($local_g);
		if (!$s){
			$s = $zbp->Config('Xd_touxiang')->s;
		}
		if (!$d){
			if ($zbp->Config('Xd_touxiang')->d == "local_g"){
				$d = $urlencode_local_g;
			}elseif($zbp->Config('Xd_touxiang')->d == "local_rg"){
				$d = $urlencode_local_rg;
			}else{
				$d = $zbp->Config('Xd_touxiang')->d;
			}
		}
		if (!$r){
			$r = $zbp->Config('Xd_touxiang')->r;
		}
		
		
		/* $atts=array( 'alt'=>'Gravatar', 'class'=>'Xd_touxiang' ) */
		if ( !empty($atts) ) {
			 $url = '<img src="' . $url . '"';
			 foreach ( $atts as $key => $val ){
			 $url .= ' ' . $key . '="' . $val . '"';
			 }
			 $url .= ' />';
		}
	
	}
	
	return $url;
	
}



function Xd_touxiang_Url(&$member) {
	global $zbp;
	if ($zbp->Config('Xd_touxiang')->local_priority && $member->ID > 0) {
		if (is_file($zbp->usersdir . 'avatar/' . $member->ID . '.png')) {
			$GLOBALS['Filter_Plugin_Mebmer_Avatar']['Xd_touxiang_Url'] = PLUGIN_EXITSIGNAL_RETURN;
			return $zbp->host . 'zb_users/avatar/' . $member->ID . '.png';
		}
	}else{
		$GLOBALS['Filter_Plugin_Mebmer_Avatar']['Xd_touxiang_Url'] = PLUGIN_EXITSIGNAL_RETURN;
		return Xd_touxiang_get( $email=$member->Email, $s = null, $d = null, $r = null,$img = null, $atts = array());
	}
}

function InstallPlugin_Xd_touxiang() {
	global $zbp;
	$zbp->Config('Xd_touxiang')->txxz = 'b';
	$zbp->Config('Xd_touxiang')->jtxz = 'b';
	$zbp->Config('Xd_touxiang')->d = 'local_rg';
	$zbp->Config('Xd_touxiang')->s = '80';
	$zbp->Config('Xd_touxiang')->dd = $zbp->host.'zb_users/avatar/0.png';
	$zbp->Config('Xd_touxiang')->r = 'G';
	$zbp->Config('Xd_touxiang')->check = false;
	$zbp->Config('Xd_touxiang')->default_url = 'https://cdn.v2ex.com/gravatar/';
	$zbp->Config('Xd_touxiang')->huiyuan = '1';
	$zbp->SaveConfig('Xd_touxiang');
}
function UninstallPlugin_Xd_touxiang() {
	global $zbp;
	$zbp->DelConfig('Xd_touxiang');
}